import java.util.*;
import danbikel.wordnet.*;


public class FeatureDictionary {

  static public int FEAT_WORD = 0;
  static public int FEAT_POS = 1;
  static public int FEAT_GPOS = 2;
  static public int FEAT_ETYPE = 3;
  static public int FEAT_MTYPE = 4;

  static public WordNet m_wn;
  static public Morphy m_morphy;
  
  static public String[][] m_vecFeatures  = {
    {},
    {"#", "$", "''", ",", "-LRB-", "-RRB-", ".", ":", "CC", "CD", "DT", "EX",
     "FW", "IN", "JJ", "JJR", "JJS", "LS", "MD", "NN", "NNP", "NNPS", "NNS", 
     "PDT", "POS", "PRP", "PRP$", "RB", "RBR", "RBS", "RP", "SYM", "TO", "UH",
     "VB", "VBD", "VBG", "VBN", "VBP", "VBZ", "WDT", "WP", "WP$", "WRB", "``"},
    {"Noun", "Verb-Active", "Verb-Passive"},
    {"PERSON", "ORGANIZATION", "GPE", "FACILITY", "LOCATION"},
    {"NAME", "NOMINAL", "PRONOUN"}
  };

  static public int ftCount()
  {
    return m_vecFeatures.length;
  }

  static public HashMap[] m_setFeatures;

  static {
    m_setFeatures = new HashMap[m_vecFeatures.length];

    for (int i = 0; i < m_vecFeatures.length; i++) {
      m_setFeatures[i] = new HashMap();
      for (int j = 0; j < m_vecFeatures[i].length; j++)
	m_setFeatures[i].put(m_vecFeatures[i][j], m_vecFeatures[i][j]);
    }

    m_wn = new WordNet("/home/pjbarrio/Dataset/WordNet-3.0/", true);
    m_morphy = new Morphy(m_wn);
  }

  static public String getAddFeature(int nType, String strFeature)
  {
    String strUnique = (String) m_setFeatures[nType].get(strFeature);
    if (strUnique == null) {
      m_setFeatures[nType].put(strFeature, strFeature);
      strUnique = strFeature;
    }

    return strUnique;
  }

  static public String getAddWordFeature(String strWord, String strPos)
  {
    String strWNPos = null;
    if (strPos.startsWith("NN"))
      strWNPos = WordNet.nounPos;
    else if (strPos.startsWith("VB"))
      strWNPos = WordNet.verbPos;
    else if (strPos.startsWith("JJ"))
      strWNPos = WordNet.adjPos;

    if (strWNPos != null) {
      String[] forms = m_morphy.morphStr(strWord, strWNPos);
      if (forms.length > 0)
	strWord = forms[0];
    }

    strWord = strWord.toLowerCase();

    String strUnique = (String) m_setFeatures[FEAT_WORD].get(strWord);
    if (strUnique == null) {
      m_setFeatures[FEAT_WORD].put(strWord, strWord);
      strUnique = strWord;
    }

    return strUnique;
  }

  static public String[] getDictFeatures(String[] vecFeatures)
  {
    assert vecFeatures.length == m_vecFeatures.length;

    String[] dictFeatures = new String[m_vecFeatures.length];

    for (int i = 0; i < vecFeatures.length; i++)
      dictFeatures[i] = vecFeatures[i];

    // compute GPOS feature
    if (dictFeatures[FEAT_POS].equals("NN") ||
	dictFeatures[FEAT_POS].equals("NNS") ||
	dictFeatures[FEAT_POS].equals("NNP") ||
	dictFeatures[FEAT_POS].equals("NNPS") ||
	dictFeatures[FEAT_POS].equals("PRP") ||
	dictFeatures[FEAT_POS].equals("PRP$"))
      dictFeatures[FEAT_GPOS] = m_vecFeatures[FEAT_GPOS][0];
    else if (dictFeatures[FEAT_POS].equals("VB") ||
	     dictFeatures[FEAT_POS].equals("VBD") ||
	     dictFeatures[FEAT_POS].equals("VBG") ||
	     dictFeatures[FEAT_POS].equals("VBP") ||
	     dictFeatures[FEAT_POS].equals("VBZ"))
      dictFeatures[FEAT_GPOS] = m_vecFeatures[FEAT_GPOS][1];
    //    else if (dictFeatures[FEAT_POS].equals("VBN"))
    //      dictFeatures[FEAT_GPOS] = m_vecFEatures[FEAT_GPOS][2];
    
    return dictFeatures;
  }


}
